package com.atfcm.link;

/**
 *
 *
 * @Author whaty
 * @create 2024/10/28 17:43
 */
public class t83删除排序链表中的重复元素 {

    /**
     * 用虚拟头节点 减少复杂度
     * @param head
     * @return
     */
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode yummy = new  ListNode(-1);
        yummy.next = head;

        ListNode cur = yummy;
        while (cur.next != null && cur.next.next != null) {
            int val = cur.next.val;
            if (val == cur.next.next.val) {
                ListNode p = cur.next.next;
                while (cur.next != null &&p.next != null && val == p.next.val) {
                    p = p.next;
                }
                cur.next= p.next;
            }else {
                cur = cur.next;
            }
        }
        return yummy.next;
    }

    class ListNode {
        public int val;
        public ListNode next;

        ListNode(int val) {
            this.val = val;
        }
    }

}
